package com.bocai.kelala.ui.travel.stroke.presenter;

import android.util.Log;

import com.bocai.kelala.bean.CheckLineBean;
import com.bocai.kelala.bean.MyAddressBean;
import com.bocai.kelala.ui.travel.stroke.contract.PublishStrokeContract;
import com.bocai.kelala.util.AESUtils;
import com.bocai.kelala.util.RetryWithDelay;
import com.bocai.mybase.base.BaseResponse;
import com.bocai.mybase.bean.OptionalSeatsBean;
import com.bocai.mybase.net.Api;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import javax.inject.Inject;

import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yuanfei on 2017/6/28.
 */

public class PublishStrokePresenter extends PublishStrokeContract.presenter {

    private final PublishStrokeContract.view view;
    private final Api api;
    public String start = "{\"content\":";
    public String end = "}";
    Subscription subscription;

    @Inject
    public PublishStrokePresenter(PublishStrokeContract.view view, Api api) {
        this.view = view;
        this.api = api;
    }

    @Override
    public void checkLine(String addressId) {
        view.showLoading("");
        api.checkLine(addressId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    String result = AESUtils.decode(baseResponse.getData());
                    Log.e("result",result);
                    CheckLineBean bean = new Gson().fromJson(result,CheckLineBean.class);
                    view.checkLineSuccess(bean);
                }else{

                    if (baseResponse.getReturnCode().equals("0001")){
                        view.showErrorTip("1");
                    }else {
                        view.showErrorTip(baseResponse.getMsg());
                    }
                }
            }
        });
    }

    @Override
    public void publishStroke(String PriceId, String DepartureTime) {
        view.showLoading("");
        api.publishStroke(PriceId,DepartureTime).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    view.publishStrokeSuccess();
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void optionalSeats() {
        view.showLoading("");
        api.optionalSeats().subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<OptionalSeatsBean>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable", e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(OptionalSeatsBean baseResponse) {
                view.stopLoading();
                if (baseResponse.getReturnCode().equals("200")) {
                    view.optionalSeatsList(baseResponse.getData());
                } else {
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void originAreas() {
        view.showLoading("");
        api.originAreas().subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable", e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                if (baseResponse.getReturnCode().equals("200")) {

                    String result = AESUtils.decode(baseResponse.getData());
                    Log.e("result",result);
                    Type type = new TypeToken<List<MyAddressBean>>(){}.getType();
                    List<MyAddressBean> beanList = new Gson().fromJson(result,type);
                    view.originAreasList(beanList);

                } else {
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void arriveAreas(String originCityId, String originAddress) {
        view.showLoading("");
        api.arriveAreas(originCityId,originAddress).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable", e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                if (baseResponse.getReturnCode().equals("200")) {

                    String result = AESUtils.decode(baseResponse.getData());
                    Log.e("result",result);
                    Type type = new TypeToken<List<MyAddressBean>>(){}.getType();
                    List<MyAddressBean> beanList = new Gson().fromJson(result,type);
                    view.arriveAreasList(beanList);

                } else {
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }
}
